package com.share.tool.mock.web;

import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.tool.mock.model.db.HttpVariable;
import com.share.tool.mock.model.vo.HttpAddOrUpdateReq;
import com.share.tool.mock.model.vo.HttpPageListReq;
import com.share.tool.mock.service.HttpVariableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 01401061 - 孙艳强
 * @date 2020/12/16 11:50
 */
@Slf4j
@Api("mork-http数据")
@RestController
@RequestMapping("/http")
public class HttpMockController {
    @Autowired
    private HttpVariableService mockHttpVariableService;

    //分页查询
    @ResponseBody
    @RequestMapping(value = "/pageList")
    public Result httpPageList(HttpPageListReq req) {
        Page page = mockHttpVariableService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("新增")
    @RequestMapping("/add")
    public Result add(HttpAddOrUpdateReq req) {
        HttpVariable record = new HttpVariable();
        BeanUtils.copyProperties(req, record);
        return Result.bool(mockHttpVariableService.save(record));
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(HttpAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        HttpVariable record = new HttpVariable();
        BeanUtils.copyProperties(req, record);
        return Result.bool(mockHttpVariableService.updateById(record));
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return Result.bool(mockHttpVariableService.removeById(id));
    }
}
